#pragma once
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <atomic>
#include <fstream>
#include <boost/algorithm/string.hpp>
namespace ns_util
{
    const std::string path = "./temp/";

    class Time_Util
    {
    public:
        static std::string GetTimeStamp()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec);
        }

        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
        }
    };
    class Path_Util
    {
    public:
        static std::string AddSuffix(const std::string &filename, const std::string &suffix)
        {
            return path + filename + suffix;
        }
        static std::string Src(const std::string &filename)
        {
            return AddSuffix(filename, ".cpp");
        }
        static std::string Exe(const std::string &filename)
        {
            return AddSuffix(filename, ".exe");
        }
        static std::string compilerErr(const std::string &filename)
        {
            return AddSuffix(filename, ".compileErr");
        }
        static std::string StdIn(const std::string &filename)
        {
            return AddSuffix(filename, ".stdin");
        }
        static std::string StdOut(const std::string &filename)
        {
            return AddSuffix(filename, ".stdout");
        }
        static std::string StdErr(const std::string &filename)
        {
            return AddSuffix(filename, ".stderr");
        }
    };
    class File_Util
    {
    public:
        static bool isFileExist(const std::string &pathname)
        {
            struct stat t;
            int ret = stat(pathname.c_str(), &t); // 获取文件的属性信息，成功返回0，失败返回-1
            return ret == 0;
        }
        static std::string UniqueFileName()
        {
            std::string ms = Time_Util::GetTimeMs();
            std::atomic<int> id(0);
            std::string unique_id = std::to_string(id);
            id++;
            return ms + "_" + unique_id;
        }
        static bool WriteFile(const std::string &pathname, const std::string &content)
        {
            std::ofstream out(pathname);
            if (!out.is_open())
                return false;
            out.write(content.c_str(), content.size());
            return true;
        }
        static bool ReadFile(const std::string &pathname, std::string *out, bool keep = false)
        {
            std::ifstream in(pathname);
            if (!in.is_open())
                return false;
            std::string line;
            while (std::getline(in, line))
            {
                (*out) += line;
                (*out) += (keep ? "\n" : "");
            }
            return true;
        }
    };

    class String_Util
    {
    public:
        /**********************************
         * str:需要分割的字符串
         * out:需要返回的字符串数组
         * sep:分割符
         **********************************/
        static void SplitString(const std::string &str, std::vector<std::string> *out, std::string sep)
        {
            boost::split((*out), str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}